/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _SOAP_RESULT_H_
#define _SOAP_RESULT_H_

#include "tansu_internal.h"
#include <wx/wx.h>
#include "tansu_util.h"
#include "tdom.h"

class SoapResult {

public:

    SoapResult(struct tdom_doc *soap)
        : soap_req(soap)
        , soap_res(NULL)
        , headers_req(NULL)
        , headers_res(NULL)
    {
        tansu_time_get_monotonic(&tv);
    }

    virtual ~SoapResult()
    {
        tdom_doc_delete(soap_req);
        tdom_doc_delete(soap_res);
        if (headers_req)
            evhttp_clear_headers(headers_req);
        if (headers_res)
            evhttp_clear_headers(headers_res);
        free(headers_req);
        free(headers_res);
    }

    void SetRequestHeaders(const struct evkeyvalq *headers)
    {
        if (headers_req) {
            evhttp_clear_headers(headers_req);
            free(headers_req);
        }
        headers_req = tansu_evkeyvalq_clone(headers);
    }

    void SetResult(struct tdom_doc *soap, const struct evkeyvalq *headers)
    {
        if (soap_res)
            tdom_doc_delete(soap_res);
        soap_res = soap;
        if (headers_res) {
            evhttp_clear_headers(headers_res);
            free(headers_res);
        }
        headers_res = tansu_evkeyvalq_clone(headers);
    }

    struct timeval *GetTimestamp() { return &tv; }
    struct tdom_doc *GetSoapRequest() { return soap_req; }
    struct tdom_doc *GetSoapResponse() { return soap_res; }
    struct evkeyvalq *GetSoapRequestHeaders() { return headers_req; }
    struct evkeyvalq *GetSoapResponseHeaders() { return headers_res; }

private:

    struct timeval tv;
    struct tdom_doc *soap_req;
    struct tdom_doc *soap_res;
    struct evkeyvalq *headers_req;
    struct evkeyvalq *headers_res;

};

WX_DECLARE_LIST(SoapResult, SoapResultList);

#endif // _SOAP_RESULT_H_

